<?php
/**
 * Created by PhpStorm.
 * Author: kaodou
 * Date: 2018/3/9
 * Time: 19:39
 */

namespace App\Api\Service\Customer\Controllers;

use Framework\BaseClass\Api\Controller;
use App\Eloquent\Ygt\Customer;
use App\Eloquent\Ygt\CustomerInvoice;
use App\Engine\Func;
use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Province;
use App\Eloquent\City;
use App\Eloquent\Area;

class InvoiceController extends Controller
{
    public function getList(){
        $userId = Func::getHeaderValueByName('userid');
        $userInfo           = DepartmentUser::getCurrentInfo( $userId );
        $companyId = isset($userInfo['company_id']) ? $userInfo['company_id'] : '';

        $customerInvoiceModel = new CustomerInvoice();
        $where = [];
        if($customerId = request('customer_id')){
            $where['customer_id'] = $customerId;
        }

        $fields = '*';

        $page = request('page');
        $limit = 10;
        $offset = ($page - 1) * $limit;

        $result = $customerInvoiceModel->getData($where, $fields, $limit, $offset, ['is_default','desc']);

        $list = $result->map(function($item, $key){
            return $this->composeInfo($item);
        })->toArray();

        $count = $customerInvoiceModel->getCount($where);

        $pagingData = [
            'page' => $page,
            'pageSize' => $limit,
            'total' => $count,
            'keyword' => ''
        ];
        return $this->pagingData($list, $pagingData);
    }

    public function getAll(){
        $userId = Func::getHeaderValueByName('userid');
        $userInfo           = DepartmentUser::getCurrentInfo( $userId );
        $companyId = isset($userInfo['company_id']) ? $userInfo['company_id'] : '';

        $customerInvoiceModel = new CustomerInvoice();
        $where = [];
        if($customerId = request('customer_id')){
            $where['customer_id'] = $customerId;
        }

        $fields = '*';

        $result = $customerInvoiceModel->getData($where, $fields, '', '', ['is_default','desc']);

        $list = $result->map(function($item, $key){
            return $this->composeInfo($item);
        })->toArray();
        return $list;

    }

    private function composeInfo($item){
        $tmp = [
            'id'=>$item->id,
            'account_name'=>$item->account_name,
            'telphone'=>$item->telphone,
            'taxpayer_number'=>$item->taxpayer_number,
            'opening_bank'=>$item->opening_bank,
            'bank_card_number'=>$item->bank_card_number,
            'customer_id'=>$item->customer_id,
            'customer_name'=>isset($item->customer->customer_name)?$item->customer->customer_name:'',
            'province_id'=>$item->province_id,
            'city_id'=>$item->city_id,
            'area_id'=>$item->area_id,
            'province_name'=>$item->province_name,
            'city_name'=>$item->city_name,
            'area_name'=>$item->area_name,
            'address'=>$item->address,
            'is_default'=>$item->is_default,
        ];
        return $tmp;
    }


    public function getInfo(){
        $where = [
            'id'=>request('id')
        ];
        $rules = [
            'id' => 'required|integer',
        ];
        $this->validateRequestParameters($where, $rules);

        $userId = Func::getHeaderValueByName('userid');
        $userInfo           = DepartmentUser::getCurrentInfo( $userId );
        $companyId = isset($userInfo['company_id']) ? $userInfo['company_id'] : '';
        $where['company_id'] = $companyId;

        $customerInvoiceModel = new CustomerInvoice();
        if($item = $customerInvoiceModel->getOneData($where)){
            $info = $this->composeInfo($item);
            return $info;
        }else{
            xThrow(ERR_CUSTOMER_IS_NOT_EXIST);
        }
    }


    public function add(){

        $params = request()->only('account_name', 'telphone', 'taxpayer_number', 'opening_bank', 'bank_card_number', 'customer_id', 'province_id', 'city_id', 'area_id', 'address', 'is_default');

        $params['customer_id'] = $params['customer_id']?$params['customer_id']:0;

        $user_id        = Func::getHeaderValueByName( 'userid' );
        $userInfo           = DepartmentUser::getCurrentInfo( $user_id );
        $companyId = isset($userInfo['company_id']) ? $userInfo['company_id'] : '';

        if($params['province_id'] == '(null)'){//过滤异常传入值
            $params['province_id'] = '';
        }

        $rules = [
            'account_name' => 'required',
            'telphone' => 'required',
            'taxpayer_number' => 'required',
            'opening_bank' => 'required',
            'bank_card_number' => 'required',
            'customer_id' => 'required|integer',
            'is_default' => 'required|integer',
            'province_id' => 'required',
        ];
        $this->validateRequestParameters($params, $rules);

        $customerInvoiceModel = new CustomerInvoice();

        if($params['is_default'] == 1){
            $customerInvoiceModel->updateOneData([
                'customer_id'=>$params['customer_id'],
                'is_default'=>1
            ], ['is_default'=>0]);
        }

        $provinceInfo = Province::where('provinceID',$params['province_id'])->first();
        $cityInfo = City::where('cityID',$params['city_id'])->first();
        $areaInfo = Area::where('areaID',$params['area_id'])->first();
        $createData = [
            'account_name' => $params['account_name'],
            'telphone' => $params['telphone'],
            'taxpayer_number' => $params['taxpayer_number'],
            'opening_bank' => $params['opening_bank'],
            'bank_card_number' => $params['bank_card_number'],
            'customer_id' => $params['customer_id'],
            'province_id' => $params['province_id'],
            'city_id' => $params['city_id'],
            'area_id' => $params['area_id'],
            'province_name' => isset($provinceInfo->province)?$provinceInfo->province:'',
            'city_name' => isset($cityInfo->city)?$cityInfo->city:'',
            'area_name' => isset($areaInfo->area)?$areaInfo->area:'',
            'address' => $params['address'],
            'is_default' => $params['is_default'],
            'company_id' => $companyId,
        ];
        $invoiceId = $customerInvoiceModel->insertOneData($createData,'id');

        $returnData = [
            'msg'=>'ok',
            'id'=>$invoiceId
        ];
        return $returnData;

    }

    public function edit(){

        $params = request()->only('id', 'account_name', 'telphone', 'taxpayer_number', 'opening_bank', 'bank_card_number', 'customer_id', 'province_id', 'city_id', 'area_id', 'address', 'is_default');

        $params['customer_id'] = $params['customer_id']?$params['customer_id']:0;

        $user_id        = Func::getHeaderValueByName( 'userid' );
        $userinfo           = DepartmentUser::getCurrentInfo( $user_id );

        $rules = [
            'id' => 'required|integer',
            'account_name' => 'required',
            'telphone' => 'required',
            'taxpayer_number' => 'required',
            'opening_bank' => 'required',
            'bank_card_number' => 'required',
            'customer_id' => 'required|integer',
            'is_default' => 'required|integer',
        ];
        $this->validateRequestParameters($params, $rules);

        $customerInvoiceModel = new CustomerInvoice();

        if($params['is_default'] == 1){
            $customerInvoiceModel->updateOneData([
                'customer_id'=>$params['customer_id'],
                'is_default'=>1
            ], ['is_default'=>0]);
        }

        $provinceInfo = Province::where('provinceID',$params['province_id'])->first();
        $cityInfo = City::where('cityID',$params['city_id'])->first();
        $areaInfo = Area::where('areaID',$params['area_id'])->first();
        $createData = [
            'account_name' => $params['account_name'],
            'telphone' => $params['telphone'],
            'taxpayer_number' => $params['taxpayer_number'],
            'opening_bank' => $params['opening_bank'],
            'bank_card_number' => $params['bank_card_number'],
            'customer_id' => $params['customer_id'],
            'province_id' => $params['province_id'],
            'city_id' => $params['city_id'],
            'area_id' => $params['area_id'],
            'province_name' => isset($provinceInfo->province)?$provinceInfo->province:'',
            'city_name' => isset($cityInfo->city)?$cityInfo->city:'',
            'area_name' => isset($areaInfo->area)?$areaInfo->area:'',
            'address' => $params['address'],
            'is_default' => $params['is_default'],
        ];
        $where = ['id'=>$params['id']];
        if(!$invoiceId = $customerInvoiceModel->updateOneData($where, $createData,'id')){
            xThrow(ERR_INVOICE_IS_NOT_EXIST);
        }

        $returnData = [
            'msg'=>'ok',
            'id'=>$invoiceId
        ];
        return $returnData;

    }

    public function del()
    {
        $invoiceId = request('id');
        $where = [
            'id'=>$invoiceId,
        ];
        $customerInvoiceModel = new CustomerInvoice();
        if($customerInvoiceModel->del($where)){
            return 'ok';
        }else{
            xThrow(ERR_INVOICE_DELETE_FAIL);
        }

    }
}